using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Ro.Norc.ServiceStackClient;
using Ro.Norc.ServiceStackClientHelloAuthAndSsl.ServiceModel.Operations;

namespace Ro.Norc.ServiceStackClientHelloAuthAndSsl
{
	public class HelloViewController : UIViewController
	{
		private Service<HelloResponse> mHelloServiceWoSsl = null;
		
		private Service<HelloResponse> mHelloServiceWSsl = null;
		
		private UIButton mWithoutSslButton = null;
		
		private UIButton mWithSslButton = null;
		
		public HelloViewController() : base()
		{
			return;
		}
		
		private void ToggleLoading(bool loading)
		{
			UIApplication.SharedApplication.NetworkActivityIndicatorVisible = loading;
			
			mWithSslButton.Enabled = !loading;
			mWithoutSslButton.Enabled = !loading;
		}
		
		private void OnServiceResult(object sender, ServiceResultArgs<HelloResponse> args)
		{
			ToggleLoading(false);
			UIAlertView response = new UIAlertView("Response", args.Result.Result, null, "Ok", null);
			response.Show();
		}
		
		private void OnServiceError(object sender, ServiceErrorArgs args)
		{
			ToggleLoading(false);
			UIAlertView error = new UIAlertView("Error", "An error occured", null, "Ok", null);
			error.Show();
		}
		
		private void OnServiceAuthPrompt(object sender, ServiceBasicHttpAuthPromptArgs args)
		{
			args.Prompt.UserName = "UserName";
			args.Prompt.Password = "Password";
		}
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
			//configure service
			mHelloServiceWoSsl = new Service<HelloResponse>("http://tests.servicestack.helloauth/ws");
			mHelloServiceWoSsl.Encoder = new ServiceEncoderJson();
			mHelloServiceWoSsl.ServiceResult += OnServiceResult;
			mHelloServiceWoSsl.ServiceError += OnServiceError;
			mHelloServiceWoSsl.BasicHttpAuthPrompt += OnServiceAuthPrompt;
			
			mHelloServiceWSsl = new Service<HelloResponse>("https://tests.servicestack.helloauth/ws");
			mHelloServiceWSsl.Encoder = new ServiceEncoderJson();
			mHelloServiceWSsl.ServiceResult += OnServiceResult;
			mHelloServiceWSsl.ServiceError += OnServiceError;
			mHelloServiceWSsl.BasicHttpAuthPrompt += OnServiceAuthPrompt;
			
			//configure buttons
			mWithoutSslButton = UIButton.FromType(UIButtonType.RoundedRect);
			mWithoutSslButton.SetTitle("Send /wo SSL", UIControlState.Normal);
			mWithoutSslButton.Frame = new RectangleF(View.Frame.Width / 2 - 150, View.Frame.Height / 2 - 15, 300, 30);
			mWithoutSslButton.TouchDown += delegate {
				ToggleLoading(true);
				mHelloServiceWoSsl.Send(new Hello() {
					Name = "Touchstack client /wo SSL"
				});
			};
			
			mWithSslButton = UIButton.FromType(UIButtonType.RoundedRect);
			mWithSslButton.SetTitle("Send /w SSL", UIControlState.Normal);
			mWithSslButton.Frame = new RectangleF(mWithoutSslButton.Frame.X, mWithoutSslButton.Frame.Y + 35, 300, 30);
			mWithSslButton.TouchDown += delegate {
				ToggleLoading(true);
				mHelloServiceWSsl.Send(new Hello() {
					Name = "Touchstack client /w SSL"
				});
			};
			
			View.BackgroundColor = UIColor.White;
			View.AddSubview(mWithoutSslButton);
			View.AddSubview(mWithSslButton);
		}
	}
}